﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AdmLaboratorios
{
    public static class FormsFactory
    {
        public static frmMDI MDI { get; set; }

        public static bool EvitaMaximizadoAutomatico { get; set; }
        public static bool UsaVentanasMaximizadas { get; set; }

        static List<Form> forms = new List<Form>() 
        {
            //new frmMDI(),
            //new frmLogin(),
            new frmGridCustomizer(),
            new frmPerfiles(),
            new frmPermisos(),
            new frmUsuarioPerfilDialog(),
            new frmUsuarios(),
            new frmIRAlta(),
            new frmIRConsulta(),
            //new frmClienteProveedor(),
            //new frmPartner(),
            new frmOPAlta(),
            new frmPartnerConsulta(),
            new frmPartner(),
            new frmOPConsulta(),
            new frmAsignacionProductos(),
            new frmIRSinAprobar(),
            new frmIRSinOP(),
            new frmIRModificacion(),
            new frmProductoMaestro(),
            new frmStockConsulta(),
            new frmValeConsumo(),
            new frmValeDevolucion(),
            new frmValeConsulta(),
            new frmCCAlta(),
            new frmCCConsulta(),
            new frmOPModificacion(),
            new frmProductoABM()
        };

        /// <summary>
        /// Instancia o retorna un formulario del tipo deseado.
        /// </summary>
        /// <typeparam name="T">El tipo de formulario a retornar.</typeparam>
        /// <returns></returns>
        public static T GetForm<T>() where T : Form, new()
        {
            Form ret;
            foreach (Form f in forms)
            {
                if (f is T)
                {
                    ret = f;

                    if (ret == null || ret.IsDisposed)
                        ret = new T();

                    ret.MdiParent = MDI;
                    ret.WindowState = FormWindowState.Normal;

                    return ret as T;
                }
            }

            //Si no lo encontre...
            throw new Exception("Falta agregar el Form al list de Forms en FormsFactory"); 
        }

        public static T ShowForm<T>() where T : Form, new()
        {
            T f = GetForm<T>();
            PrepararForm(f, MDI);
            return f;
        }

        public static void PrepararForm(System.Windows.Forms.Form f,
                                System.Windows.Forms.Form MDI)
        {
            f.MdiParent = MDI;
            f.BringToFront();
            f.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            if (!f.Visible) f.MinimumSize = f.Size;
            //if (f.Icon == null) f.Icon = MDI.Icon;
            f.Icon = MDI.Icon;

            //Si es una ventana 'maximizable', entonces no la maximizo, pero si la agrando lo mas posible.
            if (UsaVentanasMaximizadas && f.MaximizeBox && !f.Created)
            {
                f.Height = MDI.Height - 118; //125;
                f.Width = MDI.Width - ((frmMDI)MDI).barControl.Width - 25; //175; //164; // 175;
            }

            f.BackColor = System.Drawing.Color.LightBlue; //System.Drawing.Color.LightSteelBlue;
            f.Show();

            //Si es una ventana NO 'maximizable', entonces la vuelvo al estado Normal.
            if (EvitaMaximizadoAutomatico && f.WindowState == System.Windows.Forms.FormWindowState.Maximized && !f.MaximizeBox)
            {
                f.WindowState = System.Windows.Forms.FormWindowState.Normal;
            }


        }
    }
}
